﻿namespace Jsl.FxCop
{
    using Microsoft.FxCop.Sdk;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class TestRemoveEmptyTry
    {
        [TestMethod]
        public void No_problems_if_no_try()
        {
            new RemoveEmptyTry()
                .RunMethodRule(
                    () =>
                    {
                        int x = 5;
                        int y = x;
                    })
                .HasNoMoreProblems();
        }

        [TestMethod]
        public void No_problems_if_try_not_empty()
        {
            new RemoveEmptyTry()
                .RunMethodRule(
                    () =>
                    {
                        try
                        {
                            int x = 5;
                            int y = x;
                        }
                        catch
                        {
                        }
                    })
                .HasNoMoreProblems();
        }

        [TestMethod]
        public void Emtpy_try_produces_problems()
        {
            const string code = @"
public class Class
{
    public void Method()
    {
        try
        {
        }
        catch
        {
        }
    }
}";
            var assembly = new CompileHelper()
                .AddSource(code)
                .Generate(true);
            var type = TypeNodeHelper.GetTypeNode(assembly, "Class");

            new RemoveEmptyTry()
                .RunMethodRule(type, "Method")
                .HasProblem(
                    100,
                    FixCategories.NonBreaking,
                    MessageLevel.Error,
                    "Remove the unnecessary try/catch.")
                .HasNoMoreProblems();
        }
    }
}